package com.mioto.ld.module.personal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.personal.dao.FunctionDao;
import com.mioto.ld.module.personal.dao.UserDao;
import org.springframework.stereotype.Service;

import com.mioto.ld.module.personal.dao.RoleDao;
import com.mioto.ld.module.personal.model.Role;
import com.mioto.ld.module.personal.service.IRoleService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author mioto
 * @date 2024-05-09 09:30:37
 */
@Service("roleService")
public class RoleServiceImpl implements IRoleService{

    @Resource
    private RoleDao roleDao;
    @Resource
    private FunctionDao functionDao;

    @Resource
    private UserDao userDao;

    @Override
    public List<Role> findList(Role role) {
        return roleDao.findList(role);
    }

    @Override
    public int insert(Role role) {
        role.initCreateData();
        role.setDeleted(1);
        return roleDao.insertIgnoreNull(role);
    }

    @Override
    public int update(Role role) {
        return roleDao.update(role);
    }

    @Override
    public int updateIgnoreNull(Role role) {
        return roleDao.updateIgnoreNull(role);
    }

    @Override
    public Role findByColumn(String column, Object value) {
        return roleDao.findByColumn(column,value);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByColumn(String column, Object value) {
        if (StrUtil.equals(column,"id")){
            functionDao.delPermissions(Integer.parseInt(value.toString()));
            userDao.delUserRoleByRoleId(Integer.parseInt(value.toString()));
        }else {
            int roleId = roleDao.findByColumn(column,value).getId();
            functionDao.delPermissions(roleId);
            userDao.delUserRoleByRoleId(roleId);
        }
        return roleDao.deleteByColumn(column,value);
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return roleDao.batchDelete(ids);
    }


}